package com.swm.utilAPI;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 师文明
 * @data 2025/4/2 下午3:16
 **/
public class ConditionUtilAPI {

    //获取最近几天是否放量
    public static boolean getMoneyUp(JSONArray klines, double multiple, int startDay) {
        //最近几天内
        int days = 3;
        //往前推的天数
        int bedays = 3;
        boolean re = false;
        // double multiple= 5;
        for (int i = startDay; i < days + startDay; i++) {
            //今日成交额
            double v0 = Double.parseDouble(klines.get(klines.size() - i).toString().split(",")[5]);
            for (int j = 1; j < bedays; j++) {
                //j天前成交额
                double v1 = Double.parseDouble(klines.get(klines.size() - (i + j)).toString().split(",")[5]);
                if (v0 / v1 > multiple) {
                    re = true;
                    break;
                }
            }
        }
        return re;
    }

    //获取最近成交额稳定是否存在
    public static boolean getMoneyAmplitude(JSONArray klines, int startDay) {

        //最近几天内
        int days = 5;
        //往前推的天数
        int bedays = 21;

        //从哪天开始 1就是今天
        //int startDay=1;

        double max = 0;
        double min = 1000000000;
        double average = 0;
        double total = 0;

        boolean moneyflag = false;
        for (int num = startDay; num < days; num++) {
            for (int i = num; i < (num + bedays); i++) {
                String[] split = klines.get(klines.size() - i).toString().split(",");
                double money = Double.parseDouble(split[5]);
                max = Math.max(money, max);
                min = Math.min(money, min);
                total += money;
            }
            average = total / bedays;
            //十天内成交量振幅
            double v3 = (max - min) / average;
            if (v3 < 1) {
                moneyflag = true;
            }
        }
        return moneyflag;
    }

    //获取最近价格稳定是否存在
    public static boolean getPriceAmplitude(JSONArray klines, int startDay) {

        //往前推的天数
        int bedays = 30;
        double maxprice = 0;
        double minprice = 10000;
        boolean priceflag = false;
        //十天振幅

        double price0 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        for (int i = startDay + 1; i < (startDay + bedays) + 1; i++) {
            String[] split = klines.get(klines.size() - i).toString().split(",");
            double price = Double.parseDouble(split[2]);
            maxprice = Math.max(price, maxprice);
            minprice = Math.min(price, minprice);
        }
        double priceDiff = (maxprice - minprice) / price0;
        if (priceDiff < 0.1) {
            priceflag = true;
        }


        return priceflag;
    }

    //获取所有均线
    public static JSONObject getLinesAPI(String[] klines, int startDay) {
        //日线
        double totalsix1 = 0.0;
        double totalth1 = 0.0;
        double totaltw1 = 0.0;
        double totalte1 = 0.0;
        double totalf1 = 0.0;
        for (int j = 0; j < 60; j++) {
            double price = Double.parseDouble(klines[klines.length - (startDay + j)].toString().split(",")[3]);
            if (j < 5) {
                totalf1 += price;
            }
            if (j < 10) {
                totalte1 += price;
            }
            if (j < 20) {
                totaltw1 += price;

            }
            if (j < 30) {
                totalth1 += price;

            }
            totalsix1 += price;

        }
        double six1 = totalsix1 / 60;
        double th1 = totalth1 / 30;
        double tw1 = totaltw1 / 20;
        double te1 = totalte1 / 10;
        double f1 = totalf1 / 5;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("f1", f1);
        jsonObject.put("te1", te1);
        jsonObject.put("tw1", tw1);
        jsonObject.put("th1", th1);
        jsonObject.put("six1", six1);
        return jsonObject;

    }

    //近120天 价格处于低位
    public static boolean getPrice0(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(10);
    }

    //近120天 价格处于低位
    public static boolean getPriceAPI(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 10; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 < prices.get(2)/*s1==prices.get(0)*/;
    }

    //近120天 价格处于低位
    public static boolean getPrice1201(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s2 == prices.get(119)/*&&s1>prices.get(5)*/;
    }

    //近120天 价格处于低位
    public static boolean getPrice12010(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s1 < prices.get(10)/*&&s1>prices.get(5)*/;
    }

    //近120天 价格处于低位
    public static boolean getPrice1208(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s1 < prices.get(8)/*&&s1>prices.get(5)*/;
    }

    //近120天 价格处于低位
    public static boolean getPrice1205(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double s1 = Double.parseDouble(klines.get(klines.size() - startDay - 1).toString().split(",")[2]);
        double s2 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return s1 < prices.get(5)/*&&s1>prices.get(5)*/;
    }

    //近120天 价格处于低位
    public static boolean getPriceIndex(JSONArray klines, int startDay, int num) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double p1 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < num; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return p1 < prices.get(2);
    }

    //近30天 价格处于高位
    public static boolean getInsertPrice(JSONArray klines, int startDay) {
        //最近120天内收盘价集合
        List<Double> prices = new ArrayList<>();
        double p1 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[2]);
        for (int j = 0; j < 120; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            prices.add(p2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        prices.sort(Double::compare);
        return p1 > prices.get(100);
    }

    //近120天 成交量处于高位
    public static boolean getMoney(JSONArray klines, int startDay) {
        //最近120天内成交量集合
        List<Double> numbers = new ArrayList<>();
        double v1 = Double.parseDouble(klines.get(klines.size() - startDay).toString().split(",")[5]);
        for (int j = 0; j < 120; j++) {
            //成交量
            double v2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[5]);
            numbers.add(v2);
        }
        // 使用List的sort()方法和Lambda表达式进行排序
        numbers.sort(Double::compare);
        return v1 > numbers.get(100);
    }


    //获取几天内最大价格
    public static double getMaxPrice(JSONArray klines, int startDay, int days) {
        //最近120天内成交量集合
        double max = 0;
        for (int j = 0; j < days; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            max = Math.max(p2, max);
        }
        return max;
    }

    //获取几天内最小价格
    public static double getMinPrice(JSONArray klines, int startDay, int days) {
        //最近120天内成交量集合
        double min = 1000;
        for (int j = 0; j < days; j++) {
            //收盘价
            double p2 = Double.parseDouble(klines.get(klines.size() - (startDay + j)).toString().split(",")[2]);
            min = Math.min(p2, min);
        }
        return min;

    }


    //百度股票接口
    public static String getWebDataAPI(String code, int type) {
        String data = null;
        try {
            //百度股票接口
            String url = "http://api.mairui.club/hszbl/fsjy/"+code+"/dn/b997d4403688d5e66a";
            // 连接到新浪财经接口
            // String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f116,f117&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=" + type + "." + code + "&klt=101&fqt=1";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie","BIDUPSID=ED453AB33C2829FEA20329DD046E0987; PSTM=1744596337; BAIDUID=ED453AB33C2829FEEC80151D48EBADA0:FG=1; H_WISE_SIDS=61027_62325_62337_62638_62676_62863_62877_62882_62911_62919_62928_62920_62969_62950; delPer=0; PSINO=2; BAIDUID_BFESS=ED453AB33C2829FEEC80151D48EBADA0:FG=1; BDRCVFR[rYCNwgpEuDR]=mk3SLVN4HKm; BA_HECTOR=80050g8501aha1052g848l2l074sag1k03p5623; ZFY=6hGcnRiW69Xf9Wd3o2Jm6GdCainnd1xsLh:BgMkyy1DI:C; H_PS_PSSID=61027_62325_62337_62638_62676_62863_62877_62882_62911_62919_62928_62920_62969_62950_63017; BDRCVFR[S4-dAuiWMmn]=sLVIjNVtu_sfjRLnHnYnHb1gvq-UNqb; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; ab_sr=1.0.1_ZTMwMDllYThhYzI2NGFmNjQzMjM4MTQ1MWU5MjJhNWE5YTJlM2ZmZTY4YjA5N2ZkYjEwYzVkMjhjOTM3OWUxN2MwOGQyNmIwMzczMTk1YjIzYmNkZjg5MjQ5Zjg1ODY5YjQ1NDg5ODY1MTliOWU1N2IyOGE4MDc3NTFkMzJmZjY1MTBhN2Q2NTA4NWZiMzVmMmU2MjY4M2YyZjliNzFhZg==");
            // 设置连接超时时间为 10 秒
            con.setConnectTimeout(10000);
            // 设置读取超时时间为 10 秒
            con.setReadTimeout(10000);
            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            con.disconnect();
            data = response.toString();
        } catch (Exception e) {
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }

        return data;
    }


    public static JSONArray getDataFromAPI(String code, int type) {
        //"时间戳",    "时间",    "开盘","收盘","成交量","最高","最低","成交额",  "涨跌额","涨跌幅","换手率","昨收","ma5均价","ma5成交量","ma10均价","ma10成交量", "ma20均价","ma20成交量"
        //1744905600,2025-04-18,21.52,22.15,1142100,22.20,21.32,24837733.00,+0.63, +2.93,   1.05,  21.52, 21.85,  1110249,    21.38,    1609584,     22.83,    1374996
        String data = getWebDataAPI(code, type);
        JSONArray parse = JSONArray.parse(data);
        return parse;
    }

}
